define(["dojo/_base/declare", "dojo/_base/lang", "esri/geometry/Point", "esri/geometry/ScreenPoint"], function (e, t, n, i) {
    return e("Echarts3Layer", null, {
        name: "Echarts3Layer",
        _map: null,
        _ec: null,
        _geoCoord: [],
        _option: null,
        _mapOffset: [0, 0],
        constructor: function (options) {
        },
        _init: function (e, t) {
            var o = this;

            o.visible = true,
                o._elementVisible = true,
                o._map = e,
                o._ec = t,
                o.getEchartsContainer = function () {
                    return o._echartsContainer
                },
                o.getMap = function () {
                    return o._map
                },
                o.geoCoord2Pixel = function (e) {
                    var t = new n(e[0], e[1]), i = o._map.toScreen(t);
                    return [i.x, i.y]
                },
                o.pixel2GeoCoord = function (e) {

                    var t = o._map.toMap(new i(e[0], e[1]));
                    return [t.lng, t.lat]
                },
                o.initECharts = function () {
                    return o._ec = t.init.apply(o, arguments), o._ec.Geo.prototype.dataToPoint = function (e) {
                        var t = new n(e[0], e[1]), i = o._map.toScreen(t);
                        return [i.x, i.y]
                    }, o._bindEvent(), o._ec
                },
                o.getECharts = function () {
                    return o._ec
                },
                o.setOption = function (e, t) {
                    o._option = e, o._ec.setOption(e, t)
                },
                o._bindEvent = function () {
                    o._unbindEvent();
                    o._ec._zoomStartHandle = o._map.on("zoom-start", function (e) {
                        o._echartsContainer.style.visibility = "hidden"
                    }),
                        o._ec._zoomEndHandle = o._map.on("zoom-end", function (e) {
                            o._ec.resize();
                            //  o._echartsContainer.style.visibility = "visible"
                        }),

                        o._ec._panStartHandle = o._map.on("pan-start", function (e) {
                            o._echartsContainer.style.visibility = "hidden"
                        }),
                        o._ec._panEndHandle = o._map.on("pan-end", function (e) {
                            o._ec.resize();
                            //o._echartsContainer.style.visibility = "visible";
                        }),
                        o._ec._extentChangeHandle = o._map.on("extent-change", function () {
                            if(o.visible){
                                o.setScale(o._map, o._echartsContainer);
                            }

                        }),
                        o._ec._resizeHandle = o._map.on("resize", function () {
                            var e = o._echartsContainer.parentNode.parentNode.parentNode;
                            o._mapOffset = [-parseInt(e.style.left) || 0, -parseInt(e.style.top) || 0], o._echartsContainer.style.left = o._mapOffset[0] + "px", o._echartsContainer.style.top = o._mapOffset[1] + "px", setTimeout(function () {
                                o._map.resize(), o._map.reposition(), o._ec.resize()
                            }, 200), o._echartsContainer.style.visibility = "visible"
                        }),
                        o._ec._dragstartHandle = o._ec.getZr().on("dragstart", function (e) {
                        }),
                        o._ec._dragendHandle = o._ec.getZr().on("dragend", function (e) {
                        }),
                        o._ec._mousewheelHandle = o._ec.getZr().on("mousewheel", function (e) {
                            o._lastMousePos = o._map.toMap(new i(e.event.x, e.event.y));
                            var t = e.wheelDelta, n = o._map, a = n.getZoom();
                            t = t > 0 ? Math.ceil(t) : Math.floor(t), t = Math.max(Math.min(t, 4), -4), t = Math.max(n.getMinZoom(), Math.min(n.getMaxZoom(), a + t)) - a, o._delta = 0, o._startTime = null, t && n.setZoom(a + t)
                        });
                },
                o._unbindEvent = function () {
                    var esriHandleList = [o._ec._zoomEndHandle, o._ec._zoomStartHandle, o._ec._panStartHandle, o._ec._panEndHandle, o._ec._resizeHandle];
                    esriHandleList.map(function (item) {
                        if (item && typeof  item.remove == 'function') {
                            item.remove();
                        }
                        ;
                    });
                    var echartsHandleList = [o._ec._dragstartHandle, o._ec._dragendHandle, o._ec._mousewheelHandle];
                    var _render = o._ec.getZr();
                    if (_render && typeof _render.off == 'function') {
                        echartsHandleList.map(function (item) {
                            if (item) {
                                _render.off(item);
                            }
                            ;
                        });
                    }
                    ;
                },
                o.isArray = function (data) {
                    return Object.prototype.toString.call(data) === "[object Array]";
                },
                o.removeLayer = function () {
                    o._unbindEvent();
                    o._echartsContainer.remove()
                    if (o._ec) {
                        o._ec.dispose();
                    }
                    ;
                },
                o.hideLayer = function () {
                    this.visible = false;
                    this._elementVisible = false;
                    o._echartsContainer.style.visibility = "hidden";
                },
                o.showLayer = function () {
                    this.visible = true;
                    this._elementVisible = true;
                    o._echartsContainer.style.visibility = "visible";
                },
                o.setScale = (map, _element) => {
                    var scale = map.getScale();

                    if (this.options && this.options.minScale && this.options.maxScale) {
                        if (scale > this.options.minScale || scale < this.options.maxScale) {
                            _element.style.visibility = "hidden";
                            this._elementVisible = false;
                        } else {
                            _element.style.visibility = "visible";
                            this._elementVisible = true;
                        }
                        ;
                    } else if (this.options && this.options.minScale > 0) {
                        if (scale > this.options.minScale) {
                            _element.style.visibility = 'hidden';
                            this._elementVisible = false;

                        } else {
                            _element.style.visibility = 'visible';
                            this._elementVisible = true;

                        }
                        ;
                    } else if (this.options && this.options.maxScale > 0) {
                        if (scale < this.options.maxScale) {
                            _element.style.visibility = 'hidden';
                            this._elementVisible = false;

                        } else {
                            _element.style.visibility = 'visible';
                            this._elementVisible = true;

                        }
                        ;
                    } else {
                        _element.style.visibility = 'visible';
                        this._elementVisible = true;

                    }
                    ;
                }


        }
    })
});